package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.MemberMapper;
import cn.wolfcode.crm.mapper.PaywayMapper;
import cn.wolfcode.crm.mapper.RechargeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements IMemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RechargeMapper rechargeMapper;
    @Autowired
    private PaywayMapper paywayMapper;

    @Override
    public PageResult list(QueryObject qo) {
        int total=memberMapper.queryForCount(qo);
        if(total==0){
            return new PageResult();
        }
        List<Member> rows = memberMapper.queryForList(qo);
        return new PageResult(total,rows);
    }

    @Override
    public List<Member> listAll() {
        return memberMapper.selectAll();
    }

    @Override
    public void save(Member member) {
        //设置默认状态
        State initState = new State();
        initState.setId(1L);
        member.setState(initState);
        //设置门店(不同门店应该有不同的软件,这个数据应该从软件的某个配置中获取,这里用固定写法代替)
        Shop initShop = new Shop();
        initShop.setId(2L);
        member.setShop(initShop);
        //设置初始积分(这里是引用类型Integer,所以要手动设置一下,如果是int,则是默认的)
        member.setAccumulation(0);
        //设置默认来源为线下,因为当使用这个软件录入会员信息的时候,是web端,如果是会员自己注册,则是线上
        Resource initResource = new Resource();
        initResource.setId(2L);
        member.setResource(initResource);
        member.setRegistTime(new Date());
        memberMapper.insert(member);
        //第一次注册时保存一条充值记录,需要:
        Recharge recharge = new Recharge();
        recharge.setMember(member);//用户id,
        recharge.setOperateType("注册时充值");// 操作类型默认为:注册充值
        Payway payway = paywayMapper.selectByPrimaryKey(member.getPayway().getId());
        recharge.setPayType(payway.getName());//支付类型与注册支付类型一致
        //oldAccount为0
        recharge.setOldAccount(new BigDecimal("0"));
        recharge.setRechargeMoney(member.getInitAccount());//充值金额同上
        recharge.setGiftMoney(member.getGiftAccount());//赠送金额同上
        recharge.setNewAccount(member.getInitAccount().add(member.getGiftAccount()));//newAccount为三者加和
        recharge.setEmployee(member.getInputUser());//充值员工同登录的员工
        recharge.setRechargeTime(new Date());
        rechargeMapper.insert(recharge);
    }

    @Override
    public void update(Member member) {
        memberMapper.updateByPrimaryKey(member);
    }

    //删除一条
    public void remove(Long id) {
        memberMapper.deleteByPrimaryKey(id);
    }

    //高级查询
    public List<Member> queryForExport(QueryObject qo) {
        return memberMapper.queryForList(qo);
    }

    //挂失
    public void lossCard(Long id) {
        memberMapper.updateStateByMemberId(id,0L);
    }

    //取消挂失
    public void cancelLossCard(Long id) {
        memberMapper.updateStateByMemberId(id,1L);
    }

    //修改密码
    public void modifyPassword(Long id, String oldPassword, String newPassword) {
        int i = memberMapper.updatePasswordById(id, oldPassword, newPassword);
        if(i==0){
            throw new RuntimeException("原密码错误!");
        }
    }

    //重置密码
    public void resetPassword(Long id) {
        //设置默认密码
        memberMapper.resetPasswordById(id,Member.DEFAULT_PASSWORD);
    }

    //移动分组
    public void moveGroup(Long id, Long newGroupId) {
        memberMapper.moveGroup(id,newGroupId);
    }

    //打标签
    public void addTag(Long id, Long newTagId) {
        memberMapper.addTag(id,newTagId);
    }

    @Override
    public Member queryByKeyword(String keyword) {
        if(keyword==null||"".equals(keyword.trim())){
            throw  new RuntimeException("输入值无效!");
        }
        Member member = memberMapper.queryByKeyword(keyword);
        if(member==null){
            throw  new RuntimeException("无此会员!");
        }
        return member;
    }
}
